package com.orphan.marketFront.controller;


import com.orphan.marketFront.service.IGoodsSettleService;
import com.orphan.marketFront.service.IOrderService;
import com.orphan.marketFront.util.ResultCode;
import com.orphan.marketFront.util.ResultVO;
import com.orphan.util.dto.OrderDto;
import com.orphan.util.dto.OrderGoodsDto;
import com.orphan.util.dto.SettlementOrderDto;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class SettelmentController {
    @Autowired
    private IGoodsSettleService IGoodsSettleService;

    @Autowired
    private IOrderService orderService;



    @ApiOperation("发起支付请求")
    @PostMapping("/sendPayment")
    public ResultVO sendPayment(@ApiParam("订单信息") @RequestBody OrderDto orderDto){
        System.out.println(orderDto);
        String oderId = UUID.randomUUID().toString();
        orderDto.setOrderTime(new Date());
        orderDto.setOrderId(oderId);
        IGoodsSettleService.goodsSettle(orderDto);
        return ResultVO.success(oderId);
    }

    @ApiOperation("查询支付结果")
    @GetMapping("/checkPayment")
    public ResultVO checkPayment(@ApiParam("订单id") @RequestParam("orderId") String orderId){
        Integer result = IGoodsSettleService.checkPayment(orderId);
        if(result == 1){
            return ResultVO.success(ResultCode.SUCCESS);
        }
        if(result == -1){
            return ResultVO.fail(321,"支付失败");
        }
        return ResultVO.fail(320,"正在获取支付结果");
    }

}
